package org.zoomdev.zoom.dao.utils;

import org.zoomdev.zoom.dao.ConnectionExecutor;
import org.zoomdev.zoom.dao.DaoException;
import org.zoomdev.zoom.dao.ResultSetParser;
import org.zoomdev.zoom.dao.ResultSetVisitor;
import org.zoomdev.zoom.dao.impl.BuildKit;
import org.zoomdev.zoom.dao.rename.CamelRenameStrategy;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

public class DaoUtils {
    public static ResultSetParser recordListParser = rs -> BuildKit.buildList(rs, CamelRenameStrategy.DEFAULT);
    public static ResultSetParser recordParser = rs -> BuildKit.buildOne(rs, CamelRenameStrategy.DEFAULT);

    public static ResultSetParser getListParser(ResultSetParser parser) {
        return rs -> {
            List list = new ArrayList();
            while (rs.next()) {
                list.add(parser.fromResultSet(rs));
            }
            return list;
        };
    }

    public static ResultSetParser getOneParser(ResultSetParser parser) {
        return rs -> {
            if (rs.next()) {
                return parser.fromResultSet(rs);
            }
            return null;
        };
    }

    public static ConnectionExecutor getQueryExecutor(ResultSetParser parser, String sql, Object... args) {
        return connection -> {
            try (PreparedStatement ps = connection.prepareStatement(sql)) {
                prepare(ps, args);
                try (ResultSet rs = ps.executeQuery()) {
                    return parser.fromResultSet(rs);
                }
            }
        };
    }


    private static void prepare(PreparedStatement ps, Object[] args) throws SQLException {
        for (int i = 1, c = args.length + 1; i < c; ++i) {
            ps.setObject(i, args[i - 1]);
        }
    }


    public static <T> T query(Connection connection, ResultSetParser parser, String sql, Object... args) throws SQLException {
        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            prepare(ps, args);
            try (ResultSet rs = ps.executeQuery()) {
                return (T) parser.fromResultSet(rs);
            }
        }
    }

    public static void visit(Connection connection, ResultSetVisitor visitor, String sql, Object... args) throws SQLException {
        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            prepare(ps, args);
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    visitor.visit(rs);
                }
            }
        }
    }

    /**
     * 执行更新
     *
     * @param connection
     * @param sql
     * @param args
     * @return
     */
    public static int executeUpdate(Connection connection, String sql, Object... args) throws SQLException {
        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            prepare(ps, args);
            return ps.executeUpdate();
        }
    }


    public static int position2page(int position, int size) {
        if (position % size == 0) {
            return position / size + 1;
        }
        return position / size + 2;
    }


    /**
     * @param dataType
     * @return
     */
    public static Class<?> normalizeType(Class<?> dataType) {
        if (Clob.class.isAssignableFrom(dataType)) {
            return String.class;
        }
        if (Blob.class.isAssignableFrom(dataType)) {
            return byte[].class;
        }
        return dataType;
    }


    public static Class<?> dbType2JavaType(int type) {

        if (type == Types.VARCHAR || type == Types.NVARCHAR || type == Types.NCHAR || type == Types.CHAR) {
            return String.class;
        }


        if (type == Types.DECIMAL) {
            return BigDecimal.class;
        }

        if (type == Types.INTEGER || type == Types.SMALLINT || type == Types.TINYINT) {
            return Integer.class;
        }

        if (type == Types.BIGINT) {
            return BigInteger.class;
        }


        throw new DaoException("不支持的数据库类型:" + type);
    }
}
